<?php
namespace app\api\controller;
use app\common\model\SmsCode;
use app\common\validate\SmsValidate;
use app\common\service\SmsService;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("短信")
 */
class Sms extends Base
{
    protected $noNeedLogin = '*';

    /**
     * @Apidoc\Title("荣联云短信发送")
     * @Apidoc\Url("/api/sms_send")
     * @Apidoc\Method("POSTT")
     * @Apidoc\Param("mobile", type="string",require=true, desc="手机号")
     */
    public function ronglian()
    {
        checkParam(new SmsValidate(), 'send');
        $mobile = input('mobile');
        $code   = rand(1000, 9999);

        $url = 'http://auth.vrupup.com/sanguo/info/sms/index.php';

        $data = [
            'data' => http_build_query([
                'code'        => app()->env->get('sms_code'),
                'mobile'      => $mobile,
                'template_id' => app()->env->get('sms_tmp_id'),
                'param'       => json_encode([$code, 1]),
                'sms_type'    => 0
            ], JSON_UNESCAPED_UNICODE)
        ];

        $redis     = redis_instance();
        $redis_key = "{$this->redis_key}:MobileCode_Cache:{$mobile}";
        if ($redis->exists($redis_key)) {
            $mobile_code = json_decode($redis->get($redis_key), true);
            if ($mobile_code['expire_time'] > time()) err_return('60秒内请不要重复发送');
        }

        $res = json_decode(curl('POST', $url, $data), true);

        if (empty($sms)) err_return('发送失败');

        $redis->set($redis_key, json_encode(['code' => $code, 'expire_time' => time() + 60]), 5 * 60);

        if ($res['statusCode'] != 0) err_return($res['statusMsg']);

        suc_return([], '发送成功');
    }

    /**
     * @Apidoc\Title("阿里云云短信发送")
     * @Apidoc\Url("/api/sms_send_ali")
     * @Apidoc\Method("POSTT")
     * @Apidoc\Param("mobile", type="string",require=true, desc="手机号")
     */
    public function send()
    {
        checkParam(new SmsValidate(), 'send');
        $mobile      = input('mobile');
        $code        = rand(1000, 9999);
        $redis       = redis_instance();
        $key         = "{$this->redis_key}:MobileCode_Cache:{$mobile}";
        $mobile_code = $redis->get($key);
        $this->checkSend($mobile, $mobile_code);

        if ((new SmsService())->aliSms($mobile, $code)) {
            $data = [
                'code'        => $code,
                'expire_time' => time() + 60,
            ];
            $redis->set($key, json_encode($data), 5 * 60);
        }

        suc_return([], '发送成功');
    }

    /**
     * 校验发送次数
     * @Author zengxin(573908667@qq.com)
     * @param $mobile
     * @param $mobile_code
     */
    protected function checkSend($mobile, $mobile_code)
    {
        if ($mobile_code) {
            $mobile_code = json_decode($mobile_code);
            if ($mobile_code['expire_time'] < time()) err_return('每分钟只能发送一次验证码');
        }

        $start      = date('Y-m-d H:00:00', time());
        $end        = date('Y-m-d H:59:59', time());
        $hour_count = SmsCode::where(['mobile' => $mobile])->whereBetweenTime('create_time', $start, $end)->count();
        if ($hour_count >= 5) err_return('每小时只能发5条验证码');

        $start     = date('Y-m-d 00:00:00', time());
        $end       = date('Y-m-d 23:59:59', time());
        $day_count = SmsCode::where(['mobile' => $mobile])->whereBetweenTime('create_time', $start, $end)->count();
        if ($day_count >= 10) err_return('每天只能发送10条验证码');
    }

}